<template>
  <div>
    <van-cell
      title="我的频道"
      :border="false"
    >
      <van-button
        class="edit-btn"
        color="red"
        round
        plain
        size="small"
        type="primary"
        @click="isEdit=!isEdit"
      >{{ isEdit?'完成':'编辑' }}</van-button>
    </van-cell>
    <van-grid
      :column-num="4"
      :border="false"
      :gutter="10"
    >
      <van-grid-item
        v-for="(item,index) in channels"
        :key="item.id"
        class="grid-item"
        @click="addrem(index,item.id)"
      >
        <span :class="{active:index === active}">{{ item.name }}</span>
        <van-icon
          :name="isEdit && index !== 0?'close':''"
          size="17.5"
          class="edit-icon"
        />
      </van-grid-item>
    </van-grid>

    <van-cell
      title="推荐频道"
      :border="false"
    />
    <van-grid
      :column-num="4"
      direction="horizontal"
      icon-size="12.5px"
      :border="false"
      :gutter="10"
    >
      <van-grid-item
        v-for="item in recommendChannels"
        :key="item.id"
        icon="plus"
        :text="item.name"
        @click="add(item)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getchannel } from '@/api/article.js'
import { Toast } from 'vant'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage.js'
import { addchannelItem, remchannelItem } from '@/api/user.js'

export default {
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      isEdit: false,
      channelsList: []
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels() {
      // 对allChannels进行筛选
      // allChannels的每一项 是否在channels里,如果没有在channels,属于recommendChannels
      // const arr = this.allChannels.filter(item => {
      //   const flag = this.channels.some(item2 => {
      //     return item2.id === item.id
      //   })
      //   return !flag
      // })
      // return arr
      return this.channelsList.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }
  },
  created() {
    this.getchannel()
  },

  methods: {
    addrem(index, id) {
      if (this.isEdit) {
        // console.log(123)
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('act')
        }
        this.channels.splice(index, 1)
        this.del(id)
      } else {
        this.$emit('go', index)
      }
    },
    async del(id) {
      if (!this.user) return setStorage('MY-CHANNELS', this.channels)
      try {
        await remchannelItem(id)
        Toast.success('cg')
      } catch (err) {
        console.log(err)
        Toast.fail('sb')
      }
    },
    async getchannel() {
      const res = await getchannel()
      // console.log(res)
      this.channelsList = res.channels
    },
    async add(item) {
      this.channels.push(item)
      Toast.loading({
        massage: '添加中...',
        forbidClick: true,
        duration: 0
      })
      if (this.user) {
        try {
          await addchannelItem({
            id: item.id,
            seq: this.channels.length
          })
          Toast.success('cg')
        } catch (err) {
          Toast.fail('sb')
        }
        // console.log(123)
      } else {
        setStorage('MY-CHANNELS', this.channels)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.edit-btn {
  width: 104px;
  height: 48px;
}
.van-grid-item {
  width: 160px;
  height: 86px;
  border-radius: 6px;
}
/deep/ .van-grid-item__content {
  background: #f4f5f6;
}
.grid-item {
  position: relative;
  span {
    font-size: 12px;
  }
}
.edit-icon {
  position: absolute;
  top: -10px;
  right: 2px;
}
.active {
  color: red;
}
</style>
